package com.gemme.gemme.shequ;

import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.XydView;
import com.gemme.gemme.wode.XinYuanDanActivity;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.LinearSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

public class ShequPersonActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shequperson);
        initTop();
        initView();
        getDatas();
    }



    TextView tv_fans;
    TextView tv_guanzhu;
    RecyclerView mRecyclerView;
    private void initView() {
        tv_fans = findViewById(R.id.tv_fans);
        tv_guanzhu = findViewById(R.id.tv_guanzhu);
        tv_fans.setOnClickListener(this);
        tv_guanzhu.setOnClickListener(this);
        mRecyclerView = findViewById(R.id.recycler);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        //list滚动到中间位置
        new LinearSnapHelper().attachToRecyclerView(mRecyclerView);
        setrecycler();
    }
    float sh_width ;
    float screen_width;
    private void setrecycler() {
        Display defaultDisplay = getWindowManager().getDefaultDisplay();
        Point point = new Point();
        defaultDisplay.getSize(point);

        screen_width = point.x;
        sh_width = screen_width / 2;


        ShequAdapter shequAdapter = new ShequAdapter();
        mRecyclerView.setAdapter(shequAdapter);
        initListener();
       /* mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                // TODO Auto-generated method stub
                onScrolledChangedCallback();
            }
        });*/
    }

    private void initListener() {
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);



            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // dx>0则表示右滑, dx<0表示左滑, dy<0表示上滑, dy>0表示下滑

                RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
                //判断是当前layoutManager是否为LinearLayoutManager
                // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
                if (layoutManager instanceof LinearLayoutManager) {
                    LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                    //获取第一个可见view的位置
                    int firstItemPosition = linearManager.findFirstVisibleItemPosition();

                    //获取最后一个可见view的位置
                    int lastItemPosition = linearManager.findLastVisibleItemPosition();

                    if(lastItemPosition - firstItemPosition == 2){
                        currentPosition = firstItemPosition + 1;
                    }else{
                        currentPosition = firstItemPosition;
                    }
                }

                onScrolledChangedCallback();

            }
        });


    }


    int currentPosition = 0;
    private void onScrolledChangedCallback() {
        View leftView = null;
        View currentView;
        View rightView = null;
        if(currentPosition < 0)
            return;

        if (currentPosition > 0) {
            leftView = mRecyclerView.getLayoutManager().findViewByPosition(currentPosition - 1);
        }
        currentView = mRecyclerView.getLayoutManager().findViewByPosition(currentPosition);
        if (currentPosition < mRecyclerView.getAdapter().getItemCount() - 1) {
            rightView = mRecyclerView.getLayoutManager().findViewByPosition(currentPosition + 1);
        }

        try {
          /*  leftView.setScaleY(0.8f);
            currentView.setScaleY(1.1f);
            rightView.setScaleY(0.8f);*/

            control(leftView);
            control(currentView);
            control(rightView);


        }catch (Exception e){

        }
    }

    private void control(View view) {
        if(view == null)
            return;
        float left = view.getLeft();
        float right = view.getRight();
        System.out.println();

        float view_half = (left + right) / 2;
        float abs = Math.abs(view_half - sh_width);
        if(abs >=  sh_width){
            view.setScaleY(0.8f);
        }else{
            //  540
            float scale_size = 1 - Math.abs((((sh_width - view_half) / sh_width) * (1 - 0.8f)));
            view.setScaleY(scale_size);
        }
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("个人页");
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_fans:
                startActivity(new Intent(mContext,ShequFansActivity.class));
                break;

            case R.id.tv_guanzhu:
                startActivity(new Intent(mContext,ShequFansActivity.class));
                break;
                //todo
               /* case R.id.ll_shequ_detail:
                startActivity(new Intent(mContext,ShequDetailActivity.class));
                break;*/
        }
    }

    public  class ShequAdapter extends RecyclerView.Adapter<ShequAdapter.ViewHolder> {

        @NonNull
        @Override
        public ShequAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.item_shequ_person, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new ShequAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ShequAdapter.ViewHolder holder, int position) {

            if (position == 0) {
                holder.view_add_paddding.setVisibility(View.VISIBLE);
            } else {
                holder.view_add_paddding.setVisibility(View.GONE);
            }

            if (position == 10 - 1) {
                holder.view_add_paddding_right.setVisibility(View.VISIBLE);
            } else {
                holder.view_add_paddding_right.setVisibility(View.GONE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    startActivity(new Intent(mContext,ShequDetailActivity.class));
                }
            });


        }

        @Override
        public int getItemCount() {
            return 10;
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            View view_add_paddding;
            View view_add_paddding_right;

            public ViewHolder(@NonNull View itemView) {
                super(itemView);

                view_add_paddding = itemView.findViewById(R.id.view_add_paddding);
                view_add_paddding_right = itemView.findViewById(R.id.view_add_paddding_right);
            }
        }
    }

    private void getDatas() {
        String url = DataManager.BaseUrl + "community/appcusers/comunityuser";

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {

                    System.out.println("");

                }catch (Exception e){

                }
                System.out.println("");
            }
        });
    }
}